import { RoadModel, OrgModel, InspectionPlanModel, InspectionOperatModel, MaintenanceProjectModel } from '@/models'
const maintenanceProjectModel = new MaintenanceProjectModel()
const inspectionOperatModel = new InspectionOperatModel()
const roadModel = new RoadModel()
const orgModel = new OrgModel()
const inspectionPlanModel = new InspectionPlanModel()
const state = {
    loading: false,
    btnLoading: false,
    // 当前机构列表
    department: [],
    // 公路选项
    raods: [],
    // 巡查项目名称选项
    contentOption: [],
    // 项目名称
    projectName: [],
    // 设施列表
    facilityList: [],
    // 待办事项数量
    todoCount: null,
    // 是否有新消息到达, 使用一个日期类型，监听到变更刷新消息即可
    newMessage: null
}

const getters = {
    token: state => state.user.token,
    avatar: state => state.user.avatar,
    types: state => state.types,
    idUser: state => state.user.avatar.id,
    user: state => state.user.avatar
}

const mutations = {
    openLoading(state) {
        state.loading = true
    },
    closeLoading(state) {
        state.loading = false
    },
    openBtnLoading(state) {
        state.btnLoading = true
    },
    closeBtnLoading(state) {
        state.btnLoading = false
    },
    setDepartment(state, department) {
        state.department = department
    },
    setRaods(state, raods) {
        state.raods = raods
    },
    setContentOption(state, contentOption) {
        state.contentOption = contentOption
    },
    setProjectName(state, projectName) {
        state.projectName = projectName
    },
    setFacilityList(state, facilityList) {
        state.facilityList = facilityList
    },
    setTodoCount(state, todoCount) {
        state.todoCount = todoCount
    },
    setNewMessage(state) {
        state.newMessage = new Date()
    }
}

const actions = {
    async getDepartment({ commit, getters }) {
        const res = await orgModel.getUserByOrg({
            userId: getters.idUser
        })
        const users = res.map(item => {
            return {
                value: item.id,
                label: item.name,
                ...item
            }
        })
        commit('setDepartment', users)
        console.log('user')
        return users
    },
    async getRaods({ commit }) {
        const res = await roadModel.getRoads()
        console.log('res :>> ', res)
        commit('setRaods', res?.list)
    },
    async getContentList({ commit }, id) {
        console.log('id :>> ', id)
        const res = await inspectionPlanModel.getContentList({ id })
        commit('setContentOption', res)
    },
    async getProjectName({ commit }) {
        const res = await inspectionOperatModel.getContentOption()
        commit('setProjectName', res)
    },
    async getFacilityList({ commit }, data = {}) {
        const res = await maintenanceProjectModel.getFacilityList(data)
        commit('setFacilityList', res)
        return res
    }
}

export default {
    state,
    getters,
    mutations,
    actions
}
